﻿
Imports System.IO

Public Class CDATA
    Inherits xmlcontrol

    Public Const CDStart As String = "<![CDATA["
    Public Const CDEnd As String = "]]>"
    Public Shared ReadOnly CDStartLen As Byte = strlen(CDStart)
    Public Shared ReadOnly CDEndLen As Byte = strlen(CDEnd)

    Protected Overrides Function textEncoding() As xmlcontrol.textEncodingEnum
        Return textEncodingEnum.none
    End Function

    Protected Overrides Function renderBeginTag(ByVal compress As Boolean) As Boolean
        If Not renderTabs(compress) Then
            Return False
        End If

        If Not compress Then
            writeline(CDStart)
        Else
            write(CDStart)
        End If

        Return True
    End Function

    Protected Overrides Function renderEndTag(ByVal compress As Boolean) As Boolean
        If Not renderTabs(compress) Then
            Return False
        End If

        If Not compress Then
            writeline(CDEnd)
        Else
            write(CDEnd)
        End If

        Return True
    End Function

    Public Shared Widening Operator CType(ByVal this As CDATA) As String
        Return this.ToString()
    End Operator

    Public Shared Widening Operator CType(ByVal this As String) As CDATA
        Dim rtn As CDATA = Nothing
        rtn = New CDATA()
        rtn.text = this
        Return rtn
    End Operator

    Public Shared Operator +(ByVal this As CDATA, ByVal that As String) As CDATA
        this.text += that
        Return this
    End Operator

    Public Shared Operator +(ByVal this As String, ByVal that As CDATA) As String
        this += that.ToString()
        Return this
    End Operator

    Public Shared Operator +(ByVal this As CDATA, ByVal that As CDATA) As CDATA
        this.text += that.text
        Return this
    End Operator

    Public Sub New(Optional ByVal buff As Stream = Nothing)
        MyBase.New(buff)
    End Sub

    Public Shared Sub unittest()
        Dim inst As CDATA = Nothing
        inst = New CDATA()
        inst.text = "abcde[]<>()"
        Console.WriteLine(inst.ToString)
    End Sub

End Class
